package com.studio77.guessLyric.layer;

import android.graphics.Typeface;

import com.game7.fumt.MediaManager;
import com.studio77.guessLyric.MainActivity;
import com.studio77.guessLyric.R;
import com.studio77.guessLyric.core.GameSystem;
import com.studio77.guessLyric.core.StageManager;
import com.studio77.guessLyric.entity.QuestionEntity;
import com.studio77.guessLyric.scene.MainScene;
import com.studio77.guessLyric.sprite.DaanListControl;
import com.studio77.guessLyric.sprite.KeyBoard;
import com.studio77.guessLyric.util.GameUtil;
import com.studio77.guessLyric.util.offer.OfferHelper;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.sound.AudioManager;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYPoint;

/**
 * 游戏层
 * 
 * @author Dave
 * 
 */
public class GameLayer extends Layer {

	Sprite background;
	Sprite stageStar;
	Button btnCoin;
	Button btnBack;
	Sprite questionType;
	Button btnTip;
	Sprite disc;
	Sprite head;
	Label lblCurrStage;
	Label lblCoinCount;
	Label lblLyric;
	Label lblSongName;
	Label lblSingerName;
	Button btnWeixin;

	Sprite completeBackground;
	Sprite completeCoin;
	Label completeLabelDaanTitle;
	Label completeLabelDaan;
	Label completeLabelAddCoin;
	Button completeNextQuestion;

	public KeyBoard keyboard;
	public DaanListControl daanControl;

	private String currentDaanStr;

	public GameLayer() {
		// 初始化所有的控件，要保证不要重复创建，防止内存泄漏
		// 背景
		background = Sprite.make(GameSystem.tex_background);
		background.setAnchor(0, 0);
		background.setPosition(WYPoint.make(0, 0));
		this.addChild(background);

		// 游戏类型
		questionType = Sprite.make(GameSystem.tex_GameType1);
		questionType.setPosition(120, 660);
		addChild(questionType);

		// 金币
		btnCoin = Button.make(R.drawable.coin_back, R.drawable.coin_back, this,
				"onAddCoinClick");
		btnCoin.setAnchorX(1f);
		btnCoin.setClickScale(1.05f);
		btnCoin.setPosition(470, 758);
		addChild(btnCoin);

		lblCoinCount = Label
				.make(StageManager.instance.getGoldCount() + "", 32);
		lblCoinCount.setTouchEnabled(false);
		lblCoinCount.setColor(WYColor3B.make(0, 0, 0));
		lblCoinCount.setAnchorX(1f);
		lblCoinCount.setPosition(438, 757);
		addChild(lblCoinCount);

		// 返回按钮
		btnBack = Button.make(R.drawable.btn_back, R.drawable.btn_back, this,
				"OnBackClicked");
		btnBack.setAnchorX(0f);
		btnBack.setPosition(20, 760);
		btnBack.setClickScale(1.05f);
		this.addChild(btnBack);

		// 提示按钮
		btnTip = Button.make(R.drawable.btn_tip, R.drawable.btn_tip_press,
				this, "OnTipClick");
		btnTip.setAnchorX(0);
		btnTip.setPosition(336, 666);
		this.addChild(btnTip);

		// 歌手名字
		head = Sprite.make(GameSystem.tex_Head);
		head.setPosition(32, 610);
		this.addChild(head);
		lblSingerName = Label.make("林晨", 30);
		lblSingerName.setAnchorX(0f);
		lblSingerName.setPosition(61, 610);
		lblSingerName.setColor(WYColor3B.make(255, 255, 0));
		this.addChild(lblSingerName);

		// 歌曲名字
		disc = Sprite.make(GameSystem.tex_Disc);
		disc.setPosition(451, 610);
		this.addChild(disc);
		lblSongName = Label.make("林夏", 30);
		lblSongName.setAnchorX(1f);
		lblSongName.setPosition(427, 610);
		lblSongName.setColor(WYColor3B.make(255, 255, 0));
		this.addChild(lblSongName);

		btnWeixin = Button.make(R.drawable.btnshare, R.drawable.btnshare2press,
				this, "OnWeixinClick");
		btnWeixin.setAnchorX(0f);
		btnWeixin.setPosition(0, 460);
		this.addChild(btnWeixin);

		// 歌词部分
		lblLyric = Label.make("歌词\n满天都是小星星\n满满的都是爱\n想不到那么快\n遇见你什么都说出来谁叫我就是爱",
				27, "samplefont.ttf", false, 0, Texture2D.CENTER);
		lblLyric.setPosition(250, 450);
		lblLyric.setColor(WYColor3B.make(255, 255, 255));
		this.addChild(lblLyric);

		keyboard = new KeyBoard();
		keyboard.setPosition(0, 75);
		this.addChild(keyboard);

		daanControl = new DaanListControl();
		daanControl.setAnchorX(0f);
		float daanWidth = daanControl.start("我");
		daanControl.setPosition(
				(480 - daanWidth) / 2 + GameSystem.KeyWidth / 2, 250);
		this.addChild(daanControl);

		initCompleteUI();

		// 游戏关卡信息
		stageStar = Sprite.make(GameSystem.tex_stage_star);
		stageStar.setPosition(240, 750);
		stageStar.setScale(0.8f);
		addChild(stageStar);

		lblCurrStage = Label.make("", 36, Typeface.BOLD);
		lblCurrStage.setColor(WYColor3B.make(255, 255, 255));
		lblCurrStage.setPosition(240, 750);
		addChild(lblCurrStage);
	}

	/**
	 * 初始化完成界面
	 */
	private void initCompleteUI() {
		completeBackground = Sprite.make(GameSystem.tex_Complete_Background);
		completeBackground.setAnchor(0, 0);
		completeBackground.setPosition(WYPoint.make(0, 0));
		completeBackground.setVisible(false);
		this.addChild(completeBackground);

		completeLabelDaanTitle = Label.make("答案", 60, Typeface.BOLD);
		completeLabelDaanTitle.setAnchorY(1f);
		completeLabelDaanTitle.setColor(WYColor3B.make(255, 255, 255));
		completeLabelDaanTitle.setPosition(240, 630);
		completeLabelDaanTitle.setVisible(false);
		this.addChild(completeLabelDaanTitle);

		completeLabelDaan = Label.make("", 45, Typeface.BOLD);
		completeLabelDaan.setAnchorY(1f);
		completeLabelDaan.setColor(WYColor3B.make(255, 255, 255));
		completeLabelDaan.setPosition(240, 540);
		completeLabelDaan.setVisible(false);
		this.addChild(completeLabelDaan);

		completeCoin = Sprite.make(GameSystem.tex_Coin);
		completeCoin.setAnchorY(1f);
		completeCoin.setScale(0.4f);
		completeCoin.setPosition(240, 455);
		completeCoin.setVisible(false);
		this.addChild(completeCoin);

		completeLabelAddCoin = Label.make("+ 3", 45, Typeface.BOLD);
		completeLabelAddCoin.setAnchorY(1f);
		completeLabelAddCoin.setColor(WYColor3B.make(255, 255, 0));
		completeLabelAddCoin.setPosition(240, 355);
		completeLabelAddCoin.setVisible(false);
		this.addChild(completeLabelAddCoin);

		completeNextQuestion = Button.make(R.drawable.btn_next,
				R.drawable.btn_next_press, this, "onNextClicked");
		completeNextQuestion.setAnchorY(1f);
		completeNextQuestion.setPosition(240, 204);
		completeNextQuestion.setVisible(false);
		this.addChild(completeNextQuestion);
	}

	/**
	 * 开始游戏
	 */
	public void start() {
		// 关卡
		lblCurrStage.setText((StageManager.instance.getLastStage() + 1) + "");
		lblCoinCount.setText(StageManager.instance.getGoldCount() + "");

		// 隐藏Complete
		completeBackground.setVisible(false);
		completeCoin.setVisible(false);
		completeLabelAddCoin.setVisible(false);
		completeLabelDaan.setVisible(false);
		completeLabelDaanTitle.setVisible(false);
		completeNextQuestion.setVisible(false);

		// 获取关卡信息
		QuestionEntity ques = StageManager.instance.getQuestionInfo();
		if (ques.getType() == 1) {
			// 猜歌名
			questionType.setTexture(GameSystem.tex_GameType1);
			head.setVisible(true);
			lblSingerName.setVisible(true);
			lblSingerName.setText(ques.getSinger());
			disc.setVisible(false);
			lblSongName.setVisible(false);

			String lyricStr = "歌词";
			for (int i = 0; i < ques.getLyricStrList().length; i++) {
				lyricStr += "\n" + ques.getLyricStrList()[i];
			}
			lblLyric.setText(lyricStr);

			currentDaanStr = ques.getSongName();

		} else if (ques.getType() == 2) {
			// 填歌词
			questionType.setTexture(GameSystem.tex_GameType2);
			head.setVisible(true);
			lblSingerName.setVisible(true);
			lblSingerName.setText(ques.getSinger());
			disc.setVisible(true);
			lblSongName.setVisible(true);
			lblSongName.setText(ques.getSongName());

			String lyricStr = "歌词";
			for (int i = 0; i < ques.getLyricStrList().length; i++) {
				if ((ques.getGuessLyricIndex() - 1) != i) {
					lyricStr += "\n" + ques.getLyricStrList()[i];
				} else {
					lyricStr += "\n" + "___________________";
				}
			}
			lblLyric.setText(lyricStr);

			currentDaanStr = ques.getLyricStrList()[ques.getGuessLyricIndex() - 1];
		}
		float daanWidth = daanControl.start(currentDaanStr);
		daanControl.setPosition(
				(480 - daanWidth) / 2 + GameSystem.KeyWidth / 2, 250);

		// 设置键盘区域
		keyboard.resetKeyboard(StageManager.instance.getBeixuanDaan());

		// 显示富媒体
		MainActivity.instance.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				MediaManager.startAd(MainActivity.instance,
						MediaManager.CENTER_TOP,
						"dda5a106645c4a0aa0caae855e2351fe", "umeng", false);
			}
		});
	}

	public void onNextClicked() {
		AudioManager.playEffect(R.raw.enter);
		start();
	}

	public void OnWeixinClick() {
		// // 微信求助
		// GameSystem.AndroidContext.runOnUiThread(new Runnable() {
		//
		// @Override
		// public void run() {
		// UMServiceFactory.shareTo(GameSystem.AndroidContext,
		// "我使用了快速分享接口（UMServiceFactory.share）", null);
		//
		// }
		// });
		AudioManager.playEffect(R.raw.enter);
		GameSystem.AndroidContext.share();

	}

	public void onAddCoinClick() {
		// 增加金币，调用积分强
		OfferHelper.showOffer(MainActivity.instance, null);
	}

	public void OnTipClick() {
		AudioManager.playEffect(R.raw.enter);
		// 提示按钮
		if (StageManager.instance.getGoldCount() >= 50) {
			StageManager.instance.setGoldCount(StageManager.instance
					.getGoldCount() - 50);
			completeQuestion(true);
		} else {
			OfferHelper.showOffer(MainActivity.instance, "金币不足哦，是否免费获取金币？");
		}
	}

	public void OnBackClicked() {
		// 返回主菜单
		AudioManager.playEffect(R.raw.enter);
		GameUtil.switchSceneTopSlide(MainScene.make());
		MainScene.make().refreshStageIndex();
	}

	/**
	 * 完成关卡
	 * 
	 * @param b
	 *            是否使用道具
	 */
	public void completeQuestion(boolean b) {
		// 隐藏Complete
		completeBackground.setVisible(true);
		completeCoin.setVisible(true);
		completeLabelAddCoin.setVisible(true);
		completeLabelDaan.setVisible(true);
		completeLabelDaanTitle.setVisible(true);
		completeNextQuestion.setVisible(true);

		completeLabelDaan.setText(currentDaanStr);
		if (!b) {
			completeLabelAddCoin.setText("+ 3");
			StageManager.instance.setGoldCount(StageManager.instance
					.getGoldCount() + 3);
			AudioManager.playEffect(R.raw.coin);
		} else {
			completeLabelAddCoin.setText("+ 0");
		}
		StageManager.instance.turnNext();
	}

	public void updateCoinCount() {

		AudioManager.playEffect(R.raw.coin);
		lblCoinCount.setText(StageManager.instance.getGoldCount() + "");

	}

}
